﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.logChangeOilDrum
{
    public partial class Edit : EditPagebase<IChangeOilDrumContract, ChangeOilDrum>
    {
        public IOilSalesSerialNumContract OilSalesSerialNumContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<OilSalesSerialNum>(OilSalesSerialNumContract.GetResolvedTree(), 0L, ed_OilSalesSerialNum,true);
                //FineUIListHelper.BindDDL(OilSalesSerialNumContract.EntityName, this.OilSalesSerialNumContract.GetSelectList(), 0L, ed_OilSalesSerialNum,true);
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(ChangeOilDrum entity)
        {
            ed_Name.SetValue(entity.Name);
            if (!entity.OilSalesSerialNumId.IsNull())
                ed_OilSalesSerialNum.SetValue(entity.OilSalesSerialNumId);
            ed_OilPort.SetValue(entity.OilPort);
            ed_SerialNumber.SetValue(entity.SerialNumber);
            ed_OperateTime.SetValue(entity.OperateTime);
        }

        protected override OperationResult SaveData(ChangeOilDrum entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            if (ed_OilSalesSerialNum.GetValue().ToLong() == 0)
                entity.OilSalesSerialNumId = 0L;
            else
                entity.OilSalesSerialNumId = ed_OilSalesSerialNum.GetValue().ToLong();
            entity.OilPort = ed_OilPort.GetValue().ToInt();
            entity.SerialNumber = ed_SerialNumber.GetValue().ToString();
            entity.OperateTime = ed_OperateTime.GetValue().ToNullableDateTime();
            if(entity.OperateTime ==DateTime.MinValue)
                entity.OperateTime = null;
            return base.SaveData(entity);
            
        }
    }
}
